package com.daiyang.lottery.common.pojo;

import com.daiyang.lottery.common.errorcode.ErrorCode;
import com.daiyang.lottery.common.errorcode.GlobalErrorCodeConstants;
import lombok.Data;
import org.springframework.util.Assert;

import java.io.Serializable;
@Data
public class CommonResult<T>  implements Serializable {
    /**
     * 状态码
     */
    private int code;
    /**
     * 返回数据
     */
    private T data;
    /**
     * 提示信息
     * 错误信息
     */
    private String message;

    // 成功

    public static <T> CommonResult<T> success(T data ) {
        CommonResult<T> result = new CommonResult<T>();
        result.setCode(GlobalErrorCodeConstants.SUCCESS.getCode());
        result.setData(data);
        result.setMessage("");
        return result;
    }

    /**
     * 失败
     * @param message
     * @return
     */
    public static <T> CommonResult<T> error(Integer code, String message) {
        Assert.isTrue(!GlobalErrorCodeConstants.SUCCESS.getCode().equals(code), "code不是错误异常");
        CommonResult<T> result = new CommonResult<T>();
        result.setCode(code);
        result.setMessage(message);
        return result;
    }


    public static <T> CommonResult<T> error(ErrorCode errorCode) {
        return error(errorCode.getCode(), errorCode.getMsg());
    }
}
